/*
 * Copyright 2008 Neil Bacon
 * Distributed under the terms of the Mozilla Public License version 1.1
 * or the GNU General Public License version 3.
 */

package nb.util.impl;

import java.util.Arrays;
import java.util.BitSet;

import nb.util.IntMap;

public class IntMapImpl implements IntMap {
    private final int[] arr;
    private final BitSet keys = new BitSet();
    
    /**
     * @param size max valid key is size - 1
     */
    public IntMapImpl(int size) {
        arr = new int[size];
    }
    
    @Override
    public void put(int key, int value) {
        arr[key] = value;
        keys.set(key);
    }
    
    @Override
    public boolean contains(int key) {
        return keys.get(key);
    }
    
    @Override
    public int get(int key) {
        return arr[key];
    }

    @Override
    public int hashCode() {
        return Arrays.hashCode(arr) ^ keys.hashCode();
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null || getClass() != obj.getClass())
            return false;
        IntMapImpl other = (IntMapImpl) obj;
        return Arrays.equals(arr, other.arr) && keys.equals(other.keys);
    }
}
